home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / indx18eu.zip / HEXSTR.ASM < prev    next >
Assembly Source File  |  1991-03-14  |  5KB  |  133 lines

  1. CODE      SEGMENT
  2. %TITLE "Convert numeric value to a hex string"
  3.  
  4. ;-----------------------------------------------------------------------;
  5. ;                                                                       ;
  6. ;  PURPOSE :    This assembly PROC will convert any numeric value from  ;
  7. ;               a byte to a LONGINT into a Hex string.                  ;
  8. ;                                                                       ;
  9. ;               THIS CODE IS NOT MINE and all creative credit           ;
  10. ;               belongs to Borland Personnel.  All I did was enter this ;
  11. ;               the code and convert it to a TPascal MODEL.             ;
  12. ;                                                                       ;
  13. ;   AUTHOR  :   Borland International, Inc.                             ;
  14. ;                                                                       ;
  15. ;-----------------------------------------------------------------------;
  16.  
  17.                 .MODEL TPascal
  18.  
  19.                 .CODE
  20.  
  21. ;---------------  Insert PUBLIC code declarations here
  22.  
  23.                 PUBLIC    HexStr
  24.  
  25. %NEWPAGE
  26. ;---------------------------------------------------------------;
  27. ;--  FUNCTION HexStr ( VAR num                   ;            --;
  28. ;--                         count                : BYTE )     --;
  29. ;--                                              : STRING     --;
  30. ;--                                                           --;
  31. ;---------------------------------------------------------------;
  32. ;--                                                           --;
  33. ;--   INPUT :                                                 --;
  34. ;--             num       :  POINTER                          --;
  35. ;--                        - any ORDINAL variable ( no REALS )--;
  36. ;--             byteCount :  BYTE                             --;
  37. ;--                        -  a non-zero value indicating the --;
  38. ;--                           number of bytes to num occupies --;
  39. ;--                                                           --;
  40. ;---------------------------------------------------------------;
  41. ;--                                                           --;
  42. ;--  RETURN:  A STRING type with the hex equivilant of num.   --;
  43. ;--                                                           --;
  44. ;---------------------------------------------------------------;
  45. ;--                                                           --;
  46. ;--  EXAMPLE CALL FROM PASCAL:                                --;
  47. ;--                                                           --;
  48. ;--  s := HexStr ( longinteger , SizeOf ( LONGINT ) ) ;       --;
  49. ;--                                                           --;
  50. ;---------------------------------------------------------------;
  51.  
  52. HexStr PROC FAR num:DWORD,byteCount:BYTE RETURNS resultPtr:DWORD
  53.  
  54.                 LOCALS  @@
  55.  
  56.                 les     di,resultPtr    ;get address of function result
  57.  
  58.                 mov     dx,ds           ;save Turbo's DS in DX
  59.  
  60.                 lds     si,num          ;get number address
  61.  
  62.                 mov     al,byteCount    ;how many bytes?
  63.  
  64.                 xor     ah,ah           ;make a word
  65.  
  66.                 mov     cx,ax           ;keep track of bytes in CX
  67.  
  68.                 add     si,ax           ;start from MS byte of number
  69.  
  70.                 dec     si
  71.  
  72.                 shl     ax,1            ;how many digits? (byte/2)
  73.  
  74.                 cld                     ;store # digits (going forward)
  75.  
  76.                 stosb                   ;in destination string's length
  77.                                         ; byte
  78.  
  79. @@HexLoop:
  80.                 std                     ;scan number from MSB to LSB
  81.  
  82.                 lodsb                   ;get next byte
  83.  
  84.                 mov     ah,al           ;save it
  85.  
  86.                 shr     al,1            ;extract high nibble
  87.  
  88.                 shr     al,1
  89.  
  90.                 shr     al,1
  91.  
  92.                 shr     al,1
  93.  
  94.                 add     al,90h          ;special hex conversion
  95.                                         ; sequence
  96.  
  97.                 daa                     ;using ADDs and DAA's
  98.  
  99.                 adc     al,40h
  100.  
  101.                 daa                     ;nibble now converted to ASCII
  102.  
  103.                 cld                     ;store ASCII going up
  104.  
  105.                 stosb
  106.  
  107.                 mov     al,ah           ;repeat conversion for low
  108.                                         ; nibble
  109.  
  110.                 and     al,0Fh
  111.  
  112.                 add     al,90h
  113.  
  114.                 daa
  115.  
  116.                 adc     al,40h
  117.  
  118.                 daa
  119.  
  120.                 stosb
  121.  
  122.                 loop    @@HexLoop       ;keep going until done
  123.  
  124.                 mov     ds,dx           ;restore Turbo's DS
  125.  
  126.                 ret
  127.  
  128. ENDP            HexStr
  129.  
  130. ENDS            CODE
  131.  
  132.                 END                     ; END OF SOURCE
  133.